﻿using SqlSugar;
using SqlSugar_Bugs.Entities;

namespace SqlSugar_Bugs
{
    public class ReportRecordService(ISqlSugarClient db)
    {
        private readonly SqlSugarScopeProvider taskDb = db.AsTenant().GetConnectionScope(Program.TASK_DB_NAME);

        public async Task<ReportRecordEntity> CreateNew()
        {
            var entity = new ReportRecordEntity()
            {
                State = 1,
                TaskId = 12345,
                Title = "XXX报表",
                File = Path.Combine(AppContext.BaseDirectory, "TestFile", $"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.txt")
            };

            await taskDb.Insertable(entity).ExecuteReturnSnowflakeIdAsync();

            return entity;
        }

        public async Task UpdateSuccess(long id)
        {
            await taskDb.Updateable<ReportRecordEntity>().SetColumns(a => a.State == 2).Where(a => a.Id == id)
                    .ExecuteCommandAsync();
        }

        public async Task UpdateFailure(long id, string errMsg)
        {
            await taskDb.Updateable<ReportRecordEntity>().SetColumns(a => a.State == 3).SetColumns(a => a.ErrorMessage == errMsg).Where(a => a.Id == id)
               .ExecuteCommandAsync();
        }
    }
}
